package com.msb.mall.order.web;

import com.msb.common.exception.NoStockException;
import com.msb.mall.order.conf.AliPayTemplate;
import com.msb.mall.order.entity.vo.OrderConfirmVo;
import com.msb.mall.order.entity.vo.OrderResponseVo;
import com.msb.mall.order.entity.vo.OrderSubmitVo;
import com.msb.mall.order.entity.vo.PayVO;
import com.msb.mall.order.service.OrderService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;
    @Autowired
    AliPayTemplate aliPayTemplate;
    @GetMapping("/toTrade")
    public String toTrade(Model model){
        // 查询订单 确认页需要的信息
        OrderConfirmVo orderConfirmVo = null;
        try {
            orderConfirmVo = orderService.confirmOrder();
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        model.addAttribute("confirm",orderConfirmVo);
        return "confirm";
    }


    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVo vo, Model model, RedirectAttributes redirectAttributes){
        Integer code = 0;
        OrderResponseVo orderResponseVo = null;
        try {
            orderResponseVo = orderService.orderSubmit(vo);
            code = orderResponseVo.getCode();
        } catch (NoStockException e) {
            code = 2;
        }
        if(code == 0){
            model.addAttribute("orderResponseVo",orderResponseVo);
            //下单操作成功
            return "pay";
        }else{
            String msg = "订单失败";
            if(code == 1){
                msg = msg +":重复提交";
            }else if(code == 2){
                msg = msg +":锁定库存失败";
            }
            redirectAttributes.addFlashAttribute("msg",msg);
            //下单操作失败
            return "rdirect:http://localhost:8030/toTrade";
        }
    }


    @GetMapping("/orderPay/returnUrl")
    public String orderPay(@RequestParam(value = "orderSn",required = false) String orderSn,
                           @RequestParam(value = "out_trade_no",required = false) String out_trade_no,Model model){
        // 完成相关的支付操作
        if(StringUtils.isNotBlank(orderSn)){
            orderService.handleOrderComplete(orderSn);
//            orderService.updateOrderStatus(orderSn, OrderConstant.OrderStatusEnum.TO_SEND_GOODS.getCode());
        }else{
//            orderService.updateOrderStatus(out_trade_no,OrderConstant.OrderStatusEnum.TO_SEND_GOODS.getCode());
            orderService.handleOrderComplete(out_trade_no);
        }
        return "list";
    }

    /**
     * 获取订单相关信息，跳转到支付宝相关信息
     * @param orderSn
     * @return
     */
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(@RequestParam(value = "orderSn",required = false) String orderSn){
        //根据订单编号查询相关信息
        PayVO payVO = orderService.getOrderPay(orderSn);
        // 完成相关的支付操作
        return aliPayTemplate.pay(payVO);
    }
}
